package com.zyd.shiro.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyd.shiro.business.entity.Borrow;
import com.zyd.shiro.business.service.PakBorrowService;
import com.zyd.shiro.framework.object.BaseConditionVO;
import com.zyd.shiro.persistence.beans.PakBorrow;
import com.zyd.shiro.persistence.mapper.PakBorrowMapper;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Slf4j
@Service
public class PakBorrowServiceImpl implements PakBorrowService {

    @Autowired
    private PakBorrowMapper pakBorrowMapper;

    @Override
    public Borrow insert(Borrow borrow) {
        Assert.notNull(borrow, "对象不可为空！");
        borrow.getPakBorrow().setUnicode("JY"+System.currentTimeMillis());
        borrow.getPakBorrow().setAppstatus(0);
        borrow.getPakBorrow().setBostatus(0);
        borrow.getPakBorrow().setOverstatus(0);
        borrow.getPakBorrow().setUpdateTime(new Date());
        borrow.getPakBorrow().setCreateTime(new Date());
        pakBorrowMapper.insertSelective(borrow.getPakBorrow());
        return borrow;
    }

    @Override
    public void insertList(List<Borrow> borrows) {
            for(Borrow borrow:borrows){
               borrow.getPakBorrow().setUpdateTime(new Date());
               borrow.getPakBorrow().setCreateTime(new Date());
               pakBorrowMapper.insertSelective(borrow.getPakBorrow());
           }
    }


    /**
     * 根据主键字段进行删除，方法参数必须包含完整的主键属性
     *
     * @param primaryKey
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByPrimaryKey(Long primaryKey) {
        return pakBorrowMapper.deleteByPrimaryKey(primaryKey) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(Borrow borrow) {
        Assert.notNull(borrow, "对象不可为空！");
        borrow.setUpdateTime(new Date());
        return pakBorrowMapper.updateByPrimaryKey(borrow.getPakBorrow()) > 0;
    }

    @Override
    public boolean updateSelective(Borrow borrow) {
        Assert.notNull(borrow, "对象不可为空！");
        borrow.setUpdateTime(new Date());
        return pakBorrowMapper.updateByPrimaryKeySelective(borrow.getPakBorrow()) > 0;
    }

    /**
     * 根据主键字段进行查询，方法参数必须包含完整的主键属性，查询条件使用等号
     *
     * @param primaryKey
     * @return
     */

    @Override
    public Borrow getByPrimaryKey(Long primaryKey) {
        PakBorrow pakBorrow = pakBorrowMapper.selectByPrimaryKey(primaryKey);
        return null == pakBorrow ? null : new Borrow(pakBorrow);
    }


    /**
     * 根据实体中的属性进行查询，只能有一个返回值，有多个结果时抛出异常，查询条件使用等号
     *
     * @param
     * @return
     */
    @Override
    public Borrow getOneByEntity(Borrow test) {
        Assert.notNull(test, "对象不可为空！");
        PakBorrow pakBorrow = pakBorrowMapper.selectOne(test.getPakBorrow());
        return null == pakBorrow ? null : new Borrow(pakBorrow);
    }

    @Override
    public List<Borrow> listAll() {
        List<PakBorrow> pakBorrows = pakBorrowMapper.selectAll();
        if (CollectionUtils.isEmpty(pakBorrows)) {
            return null;
        }
        List<Borrow> borrows = new ArrayList<>();
        for (PakBorrow pakBorrow : pakBorrows) {
            borrows.add(new Borrow(pakBorrow));
        }
        return borrows;
    }

    @Override
    public List<Borrow> listByEntity(Borrow entity) {
        return null;
    }


    /**
     * 分页查询
     *
     * @param vo
     * @return
     */
    @Override
    public PageInfo<Borrow> findPageBreakByCondition(BaseConditionVO vo) {
        PageHelper.startPage(vo.getPageNumber(), vo.getPageSize());
        List<PakBorrow> pakBorrows = pakBorrowMapper.findPageBreakByCondition(vo);
        if (CollectionUtils.isEmpty(pakBorrows)) {
            return null;
        }
        List<Borrow> borrows = new ArrayList<>();
        for (PakBorrow pakBorrow : pakBorrows) {
            borrows.add(new Borrow(pakBorrow));
        }
        PageInfo bean = new PageInfo<PakBorrow>(pakBorrows);
        bean.setList(borrows);
        return bean;
    }
    @Override
    public void increase(PakBorrow pakBorrow) {
        pakBorrowMapper.insert(pakBorrow);
    }




}
